package com.dmcb.trade.business.crawlers.author;

import com.dmcb.common.web.conversion.JsonResult;
import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.crawlers.article.PinTuParser;
import com.dmcb.trade.business.entities.Author;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 黑米 on 2017/3/2.
 */
@Service
public class PinTuCrawler extends BaseCrawler {
    Logger logger = LoggerFactory.getLogger(JianshuCrawler.class);

    @Autowired
    private PinTuParser pinTuParser;
  /*  @Autowired
    private ArticleCrawlMapper articleCrawlMapper;*/

    @Override
    protected String url() {
        return CrawlConstant.PINTU_URL;
    }

    @Override
    protected int tag() {
        return CrawlConstant.PINTU_TAG;
    }

    @Override
    protected String avatarSelect() {
        return "a.avatar>img";
    }

    @Override
    protected String channelNameSelect() {
        return "div.title>a";
    }


    @Override
    protected String listSelect() {
        return "div.article-list>div";
    }

    @Override
    protected String titleSelect() {
        return "div.mixin-article-item>h2.title-wrap>a.title";
    }

    @Override
    protected String authorLink() {
        return null;
    }

    @Override
    protected String authorSelect() {
        return "div.title>a";
    }

    public List<String> getLink() {
        List<String> stringList = new ArrayList<String>();
        stringList.add("http://www.pintu360.com/business/");
        stringList.add("http://www.pintu360.com/people/");
        stringList.add("http://www.pintu360.com/startup/");
        stringList.add("http://www.pintu360.com/investment/");
        stringList.add("http://www.pintu360.com/baogao/");
        stringList.add("http://www.pintu360.com/chenxun/");
        stringList.add("http://www.pintu360.com/pindu/");
        return stringList;
    }

    @Override
    protected JsonResult crawl(int authorId, String link, Author author, Document doc, boolean all) {
        // 爬取文章
        int totalAll = 0;
        List<String> stringList = getLink();
        for (String src : stringList) {
            int page = 1;
            doc = webClientUtil.getDoc(src + page + "-20.html");
            if (doc == null) {
                logger.info("解析失败，link="+src + page + "-20.html");
                continue;
            }
            while (page <= CrawlConstant.CRAWL_PAGE_TOTAL) {
                JsonResult result = iterate(authorId, doc);
                page++;
                if (!result.isSuccess()) {
                    return result;
                }
                totalAll += result.getInt();
                if (!all && result.getInt() < 1) {
                    break;
                }
                doc = Jsoup.parse(webClientUtil.getPage(src + page + "-20.html").asXml());
                if(doc == null){
                    logger.info("解析失败，link="+src + page + "-20.html");
                    break;
                }
            }
        }
        return totalAll == 0 ? error(CrawlConstant.MSG_WARN_NO_UPDATE) : success(totalAll);
    }


    @Override
    protected JsonResult parse(int authorId, String link) throws Exception {
        return pinTuParser.parse(authorId, link);
    }

    @Override
    protected String getBody(Document doc) {
        return pinTuParser.getBody(doc);
    }
}
